//
//  Protocol.c
//  Wi-Fi Party remade-1
//
//  Created by Vieta Ken on 2021/4/16.
//

#import "Protocol.h"
#include <stdio.h>



struct partyPacket* parsePacket(char* receivedPacket, int length){
    struct partyPacket *parsedPacket=malloc(sizeof(struct partyPacket));
    parsedPacket->header=(struct headers *)receivedPacket;
    parsedPacket->audioPayload.start=receivedPacket+sizeof(struct headers);
    parsedPacket->audioPayload.length=length-sizeof(struct headers);
    
    return parsedPacket;
}

struct partyPacket* createPacket(int dataLength){
    char *sendContent=malloc(sizeof(struct headers)+dataLength);
    struct partyPacket *newPacket=malloc(sizeof(struct partyPacket));
    newPacket->header=(struct headers*)sendContent;
    newPacket->audioPayload.start=sendContent+sizeof(struct headers);
    newPacket->audioPayload.length=dataLength;
    
    return newPacket;
}

void freePacket(struct partyPacket *packet){
    free(packet->header);
    free(packet);
}
